/*
 * =====================================================================================
 *
 *       Filename:  list.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2012/3/8 17:37:03
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include <iostream>
#include <stdio.h>

using namespace std;

typedef int Elemtype;
typedef struct Node{
    Elemtype data;
    struct Node *next;    
};

Node *init_list(Node *head){
    head = NULL;
    return head;
}

bool insert_node(Node **head, Elemtype v){
    Node *temp, *p;
    p = *head;
    temp = (Node *)malloc(sizeof(Node));
    temp->data = v;
    temp->next = NULL;
    while(p != NULL){
        if(p->next == NULL)
            break;
        p = p->next;
    }
    if(p == NULL)
        *head = temp;
    else
        p->next = temp;
    return true;
}
void show_list(Node *head){
    Node *p = head;
    while(p != NULL){
        cout<<p -> data<<' ';
        p = p -> next;
    }
    cout<<endl;
}
Node *reverse_list(Node **head){
    Node *p1, *p2, *p3;
    if(*head == NULL)
        return *head;
    else if((*head)-> next == NULL)
        return *head;
    p1 = *head;
    p2 = p1 -> next;
    p1 -> next = NULL;
    while(p2){
        p3 = p2 -> next;
        p2 -> next = p1;
        p1 = p2;
        p2 = p3;
    }
    *head = p1;
    return *head;
}
int main(){
    int i;
    Node *head;
    head = init_list(head);
    for(i = 1; i < 20 ;i++)
      insert_node(&head, i);
    reverse_list(&head);
    show_list(head);
    return 0;
}

